package cc.rengu.oltp.service.realize.impl;

import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.SysParamMapper;
import cc.rengu.oltp.service.common.dao.impl.SysParamMapperImpl;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.utility.util.RedisUtil;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;
import com.alibaba.fastjson.JSON;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Created by 王成 on 2020/2/10
 */
public class SysParamServiceImpl implements SysParamService {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public SysParam getSysParamInfo(String instId, String paramType, String paramKey) throws Exception {
        String redisKey = instId.trim() + paramType.trim();
        List<SysParam> sysParamList = JSON.parseArray(RedisUtil.hashGet(AppParamConstant.SYS_PARAM_CACHE, redisKey), SysParam.class);
        if (null == sysParamList || sysParamList.isEmpty()) {
            SysParamMapper sysParamMapper = new SysParamMapperImpl();
            SysParam sysParam = sysParamMapper.selectSysParamByPrimaryKey(instId.trim(), paramType.trim(), paramKey.trim());
            if (null == sysParam) {
                if (!AppParamConstant.DEFAULT_INSTID.equals(instId)) {
                    /* 按照公共法人行维度查询 */
                    redisKey = AppParamConstant.DEFAULT_INSTID + paramType.trim();
                    sysParamList = JSON.parseArray(RedisUtil.hashGet(AppParamConstant.SYS_PARAM_CACHE, redisKey), SysParam.class);
                    if (null == sysParamList || sysParamList.isEmpty()) {
                        sysParam = sysParamMapper.selectSysParamByPrimaryKey(AppParamConstant.DEFAULT_INSTID, paramType.trim(), paramKey.trim());
                        if (null == sysParam) {
                            rglog.warn("使用公共法人行查询系统参数不存在或者查询到数据为未启用或者无效数据,instId:<{}>,paramType:<{}>,paramKey:<{}>", AppParamConstant.DEFAULT_INSTID, paramType, paramKey);
                            return null;
                        } else {
                            /* 只返回有效参数数据 */
                            return sysParam.getParamStatus().equals(AppParamConstant.YES) ? sysParam : null;
                        }
                    } else {
                        Optional<SysParam> sysParamOpt = sysParamList.stream().filter(item -> item.getParamKey().trim().equals(paramKey)).findFirst();
                        if (sysParamOpt.isPresent() && sysParamOpt.get().getParamStatus().equals(AppParamConstant.YES)) {
                            return sysParamOpt.get();
                        } else {
                            rglog.warn("系统参数中未配置对应的参数信息,instId:<{}>,paramType:<{}>,paramKey:<{}>", instId, paramType, paramKey);
                            return null;
                        }
                    }
                } else {
                    rglog.warn("系统参数不存在或者查询到数据为未启用或者无效数据,instId:<{}>,paramType:<{}>,paramKey:<{}>", instId, paramType, paramKey);
                    return null;
                }
            } else {
                /* 只返回有效参数数据 */
                return sysParam.getParamStatus().equals(AppParamConstant.YES) ? sysParam : null;
            }
        } else {
            Optional<SysParam> sysParamOpt = sysParamList.stream().filter(item -> item.getParamKey().trim().equals(paramKey)).findFirst();
            if (!sysParamOpt.isPresent()) {
                SysParamMapper sysParamMapper = new SysParamMapperImpl();
                SysParam sysParam = sysParamMapper.selectSysParamByPrimaryKey(instId.trim(), paramType.trim(), paramKey.trim());
                if (null == sysParam) {
                    if (!AppParamConstant.DEFAULT_INSTID.equals(instId)) {
                        /* 按照公共法人行维度查询 */
                        redisKey = AppParamConstant.DEFAULT_INSTID + paramType.trim();
                        sysParamList = JSON.parseArray(RedisUtil.hashGet(AppParamConstant.SYS_PARAM_CACHE, redisKey), SysParam.class);
                        if (null == sysParamList || sysParamList.isEmpty()) {
                            sysParam = sysParamMapper.selectSysParamByPrimaryKey(AppParamConstant.DEFAULT_INSTID, paramType.trim(), paramKey.trim());
                            if (null == sysParam) {
                                rglog.warn("使用公共法人行查询系统参数不存在或者查询到数据为未启用或者无效数据,instId:<{}>,paramType:<{}>,paramKey:<{}>", AppParamConstant.DEFAULT_INSTID, paramType, paramKey);
                                return null;
                            } else {
                                /* 只返回有效参数数据 */
                                return sysParam.getParamStatus().equals(AppParamConstant.YES) ? sysParam : null;
                            }
                        } else {
                            Optional<SysParam> sysPubParamOpt = sysParamList.stream().filter(item -> item.getParamKey().trim().equals(paramKey)).findFirst();
                            if (sysPubParamOpt.isPresent() && sysPubParamOpt.get().getParamStatus().equals(AppParamConstant.YES)) {
                                return sysPubParamOpt.get();
                            } else {
                                rglog.warn("系统参数中未配置对应的参数信息,instId:<{}>,paramType:<{}>,paramKey:<{}>", instId, paramType, paramKey);
                                return null;
                            }
                        }
                    } else {
                        rglog.warn("系统参数不存在或者查询到数据为未启用或者无效数据,instId:<{}>,paramType:<{}>,paramKey:<{}>", instId, paramType, paramKey);
                        return null;
                    }
                } else {
                    /* 只返回有效参数数据 */
                    return sysParam.getParamStatus().equals(AppParamConstant.YES) ? sysParam : null;
                }
            } else {
                SysParam sysParam = sysParamOpt.get();
                if (sysParam.getParamStatus().equals(AppParamConstant.YES)) {
                    rglog.debug("获取到的系统参数为:<{}>", JSON.toJSONString(sysParam));
                    return sysParam;
                } else {
                    rglog.warn("系统参数无效,instId:<{}>,paramType:<{}>,paramKey:<{}>", instId, paramType, paramKey);
                    return null;
                }
            }
        }
    }

    @Override
    public List<SysParam> getSysParamInfo(String instId, String paramType) throws Exception {
        String redisKey = instId.trim() + paramType.trim();
        List<SysParam> sysParamList = JSON.parseArray(RedisUtil.hashGet(AppParamConstant.SYS_PARAM_CACHE, redisKey), SysParam.class);
        if (null == sysParamList || sysParamList.isEmpty()) {
            SysParamMapper sysParamMapper = new SysParamMapperImpl();
            List<SysParam> sysParams = sysParamMapper.selectSysParamByInstIdAndParamType(instId.trim(), paramType.trim());
            if (null == sysParams || sysParams.isEmpty()) {
                if (!AppParamConstant.DEFAULT_INSTID.equals(instId)) {
                    /* 按照公共法人行维度查询 */
                    redisKey = AppParamConstant.DEFAULT_INSTID + paramType.trim();
                    sysParamList = JSON.parseArray(RedisUtil.hashGet(AppParamConstant.SYS_PARAM_CACHE, redisKey), SysParam.class);
                    if (null == sysParamList || sysParamList.isEmpty()) {
                        sysParams = sysParamMapper.selectSysParamByInstIdAndParamType(AppParamConstant.DEFAULT_INSTID, paramType.trim());
                        if (null == sysParams || sysParams.isEmpty()) {
                            rglog.warn("使用公共法人行查询系统参数不存在,instId:<{}>,paramType:<{}>", AppParamConstant.DEFAULT_INSTID, paramType);
                            return null;
                        } else {
                            /* 只返回有效参数数据 */
                            return sysParams.stream().filter(item -> item.getParamStatus().equals(AppParamConstant.YES)).collect(Collectors.toList());
                        }
                    }
                } else {
                    rglog.warn("系统参数不存在或者查询到数据为未启用或者无效数据,,instId:<{}>,paramType:<{}>", instId, paramType);
                    return null;
                }
            } else {
                /* 只返回有效参数数据 */
                return sysParams.stream().filter(item -> item.getParamStatus().equals(AppParamConstant.YES)).collect(Collectors.toList());
            }
        } else {
            rglog.debug("获取到的系统参数为:<{}>", JSON.toJSONString(JSON.toJSONString(sysParamList)));
        }
        return sysParamList;
    }
}
